using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Skill
{
    /// <summary>
    /// 冲锋类技能
    /// </summary>
    public class Dash:SkillCasterBase
    {
        public override void Start(float stateStartTime)
        {
            base.Start(stateStartTime);
            nextShootTime = 0;
            skill.unit.Data.moveState = 4;
            shootIntetval = config.GetCastParam(2);
           
            dashDistance = config.GetCastParam(0);
            targetPosition = unit.Data.position + skill.targetForward.normalized * dashDistance;
            dashSpeedValue = config.GetCastParam(1);
            dashSpeed = skill.targetForward.normalized;
            lastTime = stateStartTime;
            startTime = stateStartTime;
            duration = dashDistance / dashSpeedValue;
            isCasting = true;
        }

        public override bool IsCasting()
        {
            return isCasting;
        }

        private bool isCasting = false;
        private Vector3 targetPosition; 
        private float dashDistance;
        private Vector3 dashSpeed;
        private float dashSpeedValue;
        private float shootIntetval;
        private float nextShootTime;
        private float lastTime;
        private float startTime;
        private float duration;
        
        public override void Update(float currentTime)
        {
            var value = dashSpeedValue * (currentTime - lastTime);
          
            if (currentTime - startTime > duration)
            {
                //unit.Data.position = targetPosition;
                isCasting = false;
            }
            else
            {
                var speed = value * dashSpeed;
                var newPosition = unit.Data.position + speed;
                if (PveManager.Instance.IsInBattleRegion(newPosition))
                {
                    unit.Data.position = newPosition;
                }
            }
            
            if (nextShootTime == 0 || TimeManager.logicTime >= nextShootTime)
            {
                var offset = skill.GetBulletOffset((int)config.bullets[0].z);
                var forward = new Vector3(offset.x, 0, offset.z).normalized;
                forward.y = Random.Range(-0.1f, 0.1f);
                
                skill.CreateBulletByRotation(Vector3.zero,dashSpeed,(int)config.bullets[0].y,0,config.bullets[0].z);
                nextShootTime = TimeManager.logicTime + shootIntetval;
            }
            lastTime = currentTime;
        }
    }
}